package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSON;
import com.sc.nft.dao.MallDrawActivityPrizePoolDetailDao;
import com.sc.nft.entity.MallDrawActivityPrizePoolDetail;
import com.sc.nft.entity.dto.draw.activity.AutoReplenishmentDTO;
import com.sc.nft.enums.draw.activity.PropTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.MallDrawActivityPrizePoolDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class MallDrawActivityPrizePoolDetailServiceImpl implements MallDrawActivityPrizePoolDetailService {

    @Autowired
    private MallDrawActivityPrizePoolDetailDao mallDrawActivityPrizePoolDetailDao;

    private static final String INTEGRAL_IMG = "https://cdn.gometa.com.cn/scnft/h5_default/DDD5AA3D5D4A5307";
    private static final String NONE_IMG = "https://cdn.gometa.com.cn/scnft/h5_default/09985361BDB58CBE";

    @Override
    public boolean changePrizeCount(Long id, Integer count, boolean deduct) {
        return mallDrawActivityPrizePoolDetailDao.changePrizeCount(id, count, deduct);
    }

    @Override
    public boolean replenishPrizeCount(Long prizeId, int replenishCount) {
        MallDrawActivityPrizePoolDetail prizeDetail = mallDrawActivityPrizePoolDetailDao.getById(prizeId);
        if (prizeDetail != null) {
            prizeDetail.setRemainingPrizeCount(replenishCount * prizeDetail.getPrizeCount());
            prizeDetail.setTotalPrizeCount(replenishCount * prizeDetail.getPrizeCount());
            return mallDrawActivityPrizePoolDetailDao.updateById(prizeDetail);
        }
        return false;
    }

    @Override
    public boolean changeRemainingPrizeCountAndTotalPrizeCount(Boolean autoReplenishmentEnabled, AutoReplenishmentDTO autoReplenishmentDTO, Long id, Integer count, boolean deduct) {
        MallDrawActivityPrizePoolDetail mallDrawActivityPrizePoolDetail = mallDrawActivityPrizePoolDetailDao.getById(id);
        mallDrawActivityPrizePoolDetail.setAutoReplenishmentEnabled(autoReplenishmentEnabled);
        mallDrawActivityPrizePoolDetail.setAutoReplenishmentJson(JSON.toJSONString(autoReplenishmentDTO));
        mallDrawActivityPrizePoolDetailDao.updateById(mallDrawActivityPrizePoolDetail);
        if (count != null) {
            if (deduct) {
                Assert.isTrue(mallDrawActivityPrizePoolDetail.getRemainingPrizeCount() > count, () -> new GlobalRunTimeException("不能小于当前库存数！"));
            }
            mallDrawActivityPrizePoolDetailDao.changeTotalPrizeCount(id, count, deduct);
            mallDrawActivityPrizePoolDetailDao.changePrizeCount(id, count, deduct);
        }
        return true;
    }


    @Override
    public List<MallDrawActivityPrizePoolDetail> listByIds(List<Long> ids) {
        return mallDrawActivityPrizePoolDetailDao.listByIds(ids);
    }

    @Override
    public MallDrawActivityPrizePoolDetail getById(Long id) {
        return mallDrawActivityPrizePoolDetailDao.getById(id);
    }

    @Override
    public List<MallDrawActivityPrizePoolDetail> listByPrizePoolId(Long prizePoolId) {
        return mallDrawActivityPrizePoolDetailDao.getByPrizePoolId(prizePoolId);
    }

    @Override
    public Boolean update(MallDrawActivityPrizePoolDetail mallDrawActivityPrizePoolDetail) {
        if (mallDrawActivityPrizePoolDetail.getPropType() != PropTypeEnum.NONE) {
            mallDrawActivityPrizePoolDetail.setRemainingPrizeCount(mallDrawActivityPrizePoolDetail.getTotalPrizeCount() * mallDrawActivityPrizePoolDetail.getPrizeCount());
            mallDrawActivityPrizePoolDetail.setTotalPrizeCount(mallDrawActivityPrizePoolDetail.getTotalPrizeCount() * mallDrawActivityPrizePoolDetail.getPrizeCount());
        }
        if (mallDrawActivityPrizePoolDetail.getPropType() == PropTypeEnum.INTEGRAL) {
            mallDrawActivityPrizePoolDetail.setPropName("星球碎片");
            mallDrawActivityPrizePoolDetail.setPropImg(INTEGRAL_IMG);
            mallDrawActivityPrizePoolDetail.setPoolId(null);
        } else if (mallDrawActivityPrizePoolDetail.getPropType() == PropTypeEnum.NONE) {
            mallDrawActivityPrizePoolDetail.setPropName("感谢参与");
            mallDrawActivityPrizePoolDetail.setPropImg(NONE_IMG);
            mallDrawActivityPrizePoolDetail.setPoolId(null);
        }
        return mallDrawActivityPrizePoolDetailDao.updateById(mallDrawActivityPrizePoolDetail);
    }

}
